package util;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.OptionalDataException;
import java.net.Socket;
import model.Message;
import chui.ZoneMonitor;



/**
 * @author rpitts@gmu.edu 
 *
 */
public class SingleSocketProcess extends Thread{

	private Socket sock;		// Socket connect to client
	private ZoneMonitor zM = null;
	
	public SingleSocketProcess (ZoneMonitor zM, Socket sock){
		this.sock = sock;
		this.zM = zM;
		this.start();
	}
	
	public void run () {

		System.out.println("Starting Single Socket Process to handle inbound request");
		
		// Setup for serialized object retrieval
		Message message = null;
		ObjectInputStream ois = null;
		try {
			ois = new ObjectInputStream(sock.getInputStream());
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		
		//
		// Retrieve serialized object (unMarshal)
		//
		try {
			message = (Message) ois.readObject();

			// Process input
			zM.receiveRemoteMessage(message);

		} catch (OptionalDataException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		// Close object input stream
		try {
			ois.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		// Close socket
		try {
			sock.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
	/*
	private static String escapedAndQuoted (String theValue) {
		return '\"' + theValue + '\"';
	}
	*/
	//private static String escapedAndQuoted (Long theValue) {
	//	return escapedAndQuoted(String.valueOf(theValue));
	//}
	
}
